import request from '@/utils/request';
import type { ApiResponse, Pagination, PageResult } from '@/types/api';
import type { AxiosResponse } from 'axios';

// 订单状态枚举
export type OrderStatus = 'pending' | 'paid' | 'shipped' | 'completed' | 'cancelled';

// 订单详情项 DTO
export interface OrderItemVO {
  id: number;
  itemId: number;
  name: string;
  image: string;
  price: number; // 价格，单位为分
  quantity: number;
  spec?: string; // 商品规格
}

// 订单详情视图对象
export interface OrderDetailVO {
  id: number;
  orderId: string;
  itemId: number;
  name: string;
  image: string;
  price: number; // 价格，单位为分
  num: number; // 购买数量
  spec?: string; // 商品规格
}

// 订单视图对象
export interface OrderVO {
  id: number;
  originalId: string; // 原始字符串ID，用于API调用
  userId: number;
  orderNumber: string;
  status: OrderStatus;
  totalAmount: number; // 总金额，单位为分
  totalFee: number; // 后端返回的实际总金额字段
  totalItems: number;
  createTime: string;
  payTime?: string;
  deliveryTime?: string;
  receiveTime?: string;
  cancelTime?: string;
  items: OrderDetailVO[];
}

// 分页查询订单请求参数
export interface OrderQueryParam extends Pagination {
  status?: OrderStatus | 'all';
  orderNumber?: string;
}

// 订单明细条目 DTO
export interface OrderDetailDTO {
  itemId: number; // 商品id
  num: number; // 商品购买数量
}

// 订单创建表单 DTO
export interface OrderFormDTO {
  addressId: number; // 收货地址id
  paymentType: number; // 支付类型
  details: OrderDetailDTO[]; // 下单商品列表
}

/**
 * 创建订单
 * POST /orders
 */
export function createOrder(orderForm: OrderFormDTO): Promise<AxiosResponse<ApiResponse<string>>> {
  return request({
    url: '/orders',
    method: 'post',
    data: orderForm
  });
}

/**
 * 分页查询用户订单
 * GET /orders
 */
export function queryMyOrders(params: OrderQueryParam): Promise<AxiosResponse<ApiResponse<PageResult<OrderVO>>>> {
  return request({
    url: '/orders',
    method: 'get',
    params
  });
}

/**
 * 查询订单详情
 * GET /orders/{id}
 */
export function queryOrderDetail(id: string): Promise<AxiosResponse<ApiResponse<OrderVO>>> {
  return request({
    url: `/orders/${id}`,
    method: 'get'
  });
}

/**
 * 查询订单详情列表
 * GET /orders/{id}/details
 */
export function queryOrderDetails(id: string): Promise<AxiosResponse<ApiResponse<OrderDetailVO[]>>> {
  return request({
    url: `/orders/${id}/details`,
    method: 'get'
  });
}

/**
 * 取消订单
 * DELETE /orders/{orderId}
 */
export function cancelOrder(orderId: string): Promise<ApiResponse<void>> {
  return request({
    url: `/orders/${orderId}`,
    method: 'delete'
  });
}

/**
 * 标记订单已支付
 * PUT /orders/{orderId}/pay
 */
export function payOrder(orderId: string): Promise<ApiResponse<void>> {
  return request({
    url: `/orders/${orderId}/pay`,
    method: 'put'
  });
}

/**
 * 确认收货
 * PUT /orders/{orderId}/confirm
 */
export function confirmReceipt(orderId: string): Promise<ApiResponse<void>> {
  return request({
    url: `/orders/${orderId}/confirm`,
    method: 'put'
  });
}

/**
 * 申请退款 (虽然文档有，但目前前端不实现)
 * PUT /orders/{orderId}/refund
 */
// export function applyRefund(orderId: number): Promise<ApiResponse<void>> {
//   return request({
//     url: `/orders/${orderId}/refund`,
//     method: 'put'
//   });
// } 